package cn.itsource.hrm.service.impl;

import cn.itsource.hrm.constants.SystemConstants;
import cn.itsource.hrm.domain.Employee;
import cn.itsource.hrm.domain.Tenant;
import cn.itsource.hrm.dto.EnteringDTO;
import cn.itsource.hrm.mapper.EmployeeMapper;
import cn.itsource.hrm.mapper.MealMapper;
import cn.itsource.hrm.mapper.TenantMapper;
import cn.itsource.hrm.service.ITenantService;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luobiao
 * @since 2020-02-20
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private MealMapper mealMapper;

    @Override
    public void entering(EnteringDTO enteringDTO) {
        //当前入驻时间
        Date date = new Date();
        //封装tenant剩余数据
        Tenant tenant = enteringDTO.getTenant();
        tenant.setRegisterTime(date);
        tenant.setState(false);
        //新增机构
        baseMapper.insert(tenant);

        Employee employee = enteringDTO.getEmployee();
        employee.setInputTime(date);
        employee.setState(SystemConstants.STATE_NORMAL);
        employee.setTenantId(tenant.getId());
        //员工类型
        employee.setType(SystemConstants.TYPE_ADMIN);
        //新增管理员
        employeeMapper.insert(employee);

        //将套餐和机构id存入中间表，建立关系
        mealMapper.insertrelationAndTenant(enteringDTO.getMealId(),tenant.getId(),SystemConstants.STATE_UNPAID);
    }
}
